const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    base_url: 'https://vip.join.ink/',
    activity_list: [],
    goods_list_hot: [],
    goods_list_new: [],
    qg_sale_list: [],
    active: 1,
    cardCur: 0,
    is_show: 2,
    agreement: '',
    page: 1
  },

  comfirm() {
    this.setData({
      is_show: 2
    })
    wx.setStorageSync('is_show', 2)
  },


  go_search() {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },



  fetch() {
    this.get_activity()
    this.get_goods_list()
    // this.get_rush_purchase_list()


    let is_show = wx.getStorageSync('is_show')
    if (is_show) {
      this.setData({
        is_show
      })
    } else {
      wx.setStorageSync('is_show', 1)
      this.setData({
        is_show: 1
      })
    }
  },
  //抢购商品列表
  // get_rush_purchase_list() {
  //   app.request('/getsaleList', {
  //     data:{
  //       qgpage:1
  //     }
  //   }).then(res => {
  //     if (res.data.code == 200) {
  //       console.log(res);
  //       this.setData({
  //         qg_sale_list: res.data.qg_sale_list
  //       })
  //       // console.log(this.data.activity_list);
  //     }
  //   }).catch(err => {
  //     console.log(err);
  //   })
  // },
  // 活动列表
  get_activity() {
    app.request('/get_activity', {}).then(res => {
      if (res.data.code == 200) {
        // console.log(res);
        this.setData({
          activity_list: res.data.data
        })
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
        // console.log(this.data.activity_list);
      }
    }).catch(err => {
      console.log(err);
    })
  },
  // 商品列表
  get_goods_list() {
    var that = this
    app.request('/online_getsaleList', {}).then(res => {
      if (res.data.code == 200) {
        // console.log(res);
        this.setData({
          goods_list_new: res.data.sale_info.data
        })
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
        // console.log(this.data.goods_list_new);
      }
    }).catch(err => {
      console.log(err);
    })


    app.request('/online_getsaleList', {
      data: {
        is_hot: 1,
        page: that.data.page
      }
    }).then(res => {
      if (res.data.code == 200) {
        console.log(res);
        this.setData({
          goods_list_hot: res.data.sale_info.data
        })
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
        console.log(this.data.goods_list_hot);
      }
    })
  },

  search(e) {},

  tab_click(e) {
    let flg = e.currentTarget.dataset.index
    this.setData({
      active: flg
    })
  },
  purchase_list() {
    wx.navigateTo({
      url: '/pages/rush_purchase/rush_purchase',
    })
  },
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },

  go_map() {
    wx.navigateTo({
      url: '/pages/map/map',
    })
  },

  //协议
  agreement() {
    var that = this
    app.request("/get_user_agreement", {
      method: "get",
      data: {
        type: 2
      }
    }).then(res => {
      console.log(res);
      that.setData({
        agreement: res.data.data
      })
      // let des = that.data.agreement;
      // WxParse.wxParse('des','html',des,this,5);
    })
  },

  // ********************
  onLoad: function (options) {
    var that = this
    that.fetch()
    that.agreement();
    that.check_token()
    console.log(options, 'options');
    let scene = decodeURIComponent(options.scene);
    if (scene && scene != 'undefined') {
      wx.showToast({
        title: scene,
      })
      app.request("/user/doPageBinding", {
        method: "post",
        data: {
          code: scene
        }
      }).then(res => {
        console.log(res);
      })

    }
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log(res)
      },
      fail(e) {
        console.log(e)
      }
    })
  },
  //刷新
  onRefresh() {
    var that = this
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '正在刷新...',
    })
    that.fetch()
    that.agreement();
    that.check_token()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
  },

  onShow: function () {
    var that = this

  },
  onReachBottom: function () {
    var that = this
    //上拉加载
    wx.showLoading({
      title: '正在加载',
    })

    var that = this
    //加载次数加一
    this.setData({
      page: this.data.page + 1
    })
    app.request('/online_getsaleList', {
      data: {
        page: that.data.page
      }
    }).then(res => {
      if (res.data.code == 200) {
        // console.log(res);
        //组合获取的数据

        if (res.data.sale_info.data != '') {
          for (var i = 0; i < res.data.sale_info.data.length; i++) {

            that.data.goods_list_hot.push(res.data.sale_info.data[i])
          }

          this.setData({
            goods_list_hot: that.data.goods_list_hot
          })
        } else {
          wx.showLoading({
            title: '加载完毕',
          })
        }
      }
    })
    app.request('/online_getsaleList', {
      data: {
        page: that.data.page
      }
    }).then(res => {
      if (res.data.code == 200) {
        // console.log(res);
        //组合获取的数据

        if (res.data.sale_info.data != '') {
          for (var i = 0; i < res.data.sale_info.data.length; i++) {

            that.data.goods_list_new.push(res.data.sale_info.data[i])
          }

          this.setData({
            goods_list_new: that.data.goods_list_new
          })
        } else {
          wx.showLoading({
            title: '加载完毕',
          })
        }
      }
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)
  },

  //校验缓存中的token 和 通过openid拿到的token进行比对
  check_token() {
    var that = this
    var user_info_token = wx.getStorageSync('token');
    //获取缓存中的openid或者是全局当中的openid变量 通过指定openid查询后台的token 
    wx.request({
      url: 'https://vip.join.ink/api/get_token',
      data: {
        openid: wx.getStorageSync('openid'), //获取缓存中的openid openid不会变
        token: user_info_token
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 200) {
          //此时重新设置新的token作为缓存对象
          wx.setStorageSync('token', res.data.api_token)
          wx.setStorageSync('token_is_agreement', res.data.data) //一致情况下为1
        } else {
          //缓存中的token和后台当中的token不一致  需要重新授权登录
          wx.removeStorageSync('token')
          if (user_info_token != '') {
            wx.showModal({
              title: '提示',
              content: 'token失效，请重新登录！',
              showCancel: false
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/personal/personal',
              })
            }, 2000);
          }

        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this
    console.log(res, '88888888888888888888')
    if (res.from === 'button') {
      console.log("来自页面内转发按钮");
      console.log(res.target);
    } else {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面的对象
      var url = currentPage.route //当前页面url
      console.log(url, '888888888888')
      console.log("来自右上角转发菜单")
      return {
        title: '分享至朋友圈',
        path: url,
        success: (res) => {
          console.log("转发成功", res);
        },
        fail: (res) => {
          console.log("转发失败", res);
        }
      }
    }

  }
})